package servlet.user;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.UserDao;
import entity.User;
import net.sf.json.JSONObject;

public class UserLoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7970262764632281304L;

	public UserLoginServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		HttpSession session = request.getSession();
		JSONObject jsonObject = new JSONObject();
		UserDao userDao = new UserDao();
		User user = null;
		
		user = userDao.queryOneUser(userName, password);
		if(user != null && user.getStatus().equals("正常")) {
			jsonObject.put("retCode", "1");
			session.setAttribute("user", user);
		} else {
			jsonObject.put("retCode", "0");
			jsonObject.put("msg", "不存在此用户或用户不可用");
		}
		
		out.print(jsonObject.toString());
		out.close();
	}

	public void init() throws ServletException {
	}

}
